package com.atguigu.kaiser;

/**
 * @program: IDEAcode
 * @description:kaiser加密与解密
 * @author: Mr.zjx
 * create: 2021-08-04-14-47
 **/
public class KaiserDemo {
    public static void main(String[] args) {
        String original = "Hello word";
        int key = 3;
        String encryptKaiser = encryptKaiser(original, key);
        System.out.println("加密: " + encryptKaiser);
        String decryptKaiser = decryptKaiser(encryptKaiser, key);
        System.out.println("解密: "+decryptKaiser);
    }

    //解密
    public static String decryptKaiser(String encryptedData,int key){
        char[] chars = encryptedData.toCharArray();
        StringBuilder sb = new StringBuilder();
        for (char aChar : chars){
            int asciiCode = aChar;//转为ASCII码
            asciiCode -= key;//偏移数据
            char result = (char) asciiCode;
            sb.append(result);//转数组
        }
        return sb.toString();
    }

    //加密
    public static String encryptKaiser(String original,int key){
        char[] chars = original.toCharArray();
        StringBuilder sb = new StringBuilder();
        for (char aChar: chars){
            int asciiCode = aChar;
            asciiCode += key;
            char result = (char)asciiCode;
            sb.append(result);
        }
        return sb.toString();
    }
}
